package cn.hubujyjs.lp.controller;

import cn.hubujyjs.lp.domain.FileTree;
import cn.hubujyjs.lp.service.FileService;
import cn.hubujyjs.lp.service.FileTreeService;
import cn.hubujyjs.lp.service.OssService;
import cn.hubujyjs.lp.utils.R;
import cn.hubujyjs.lp.vo.FileStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @Autowired
    private FileTreeService fileTreeService;
    @Autowired
    private OssService ossService;

    /**
     * 目录
     * @return 目录列表
     */
    @PostMapping("/tree")
    public R<List<FileTree>> treeR(){
        return fileTreeService.tree();
    }

    /**
     * 文件上传
     * @param path 路径
     * @param file 文件
     * @return R
     */
    @PostMapping("/upload")
    public R update(String path,@RequestPart MultipartFile file){
        return fileTreeService.upload(path,file);
    }

    /**
     * 预览md文件
     * @return md文件内容
     */
    @PostMapping("/preview")
    public R<String> preview(Integer fileId){
        return fileService.preview(fileId);
    }

    /**
     * 文件下载
     * @param fileId 文件id
     * @return 文件url
     */
    @PostMapping("/download")
    public R<String> download(Integer fileId){
        return fileService.download(fileId);
    }

    /**
     * 文件统计
     * @return 统计对象
     */
    @GetMapping("/statistics")
    public R<FileStatistics> statisticsR(){
        return fileService.statistics();
    }

    @PostMapping("uploadAvatar")
    public R<String> uploadAvatar(@RequestPart("file") MultipartFile file){
        String s = ossService.uploadPicture(file);
        return R.ok(s);
    }
}
